package com.swt.shoppingmall.home.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.swt.shoppingmall.R;
import com.swt.shoppingmall.home.bean.ResultBeanData;
import com.swt.shoppingmall.utils.Constants;

import java.util.List;

/**
 * 新品推荐适配器
 * Created by swt on 2016/12/25.
 */
public class RecommendGridViewAdapter extends BaseAdapter {
    private Activity mActivity;
    private List<ResultBeanData.ResultBean.RecommendInfoBean> recommend_info;

    public RecommendGridViewAdapter(Activity activity, List<ResultBeanData.ResultBean.RecommendInfoBean> recommend_info) {
        this.mActivity = activity;
        this.recommend_info = recommend_info;
    }

    @Override
    public int getCount() {
        return recommend_info == null ? 0 : recommend_info.size();
    }

    @Override
    public Object getItem(int position) {
        return recommend_info.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (holder == null) {
            convertView = View.inflate(mActivity, R.layout.item_recommend_grid_view, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ResultBeanData.ResultBean.RecommendInfoBean recommendInfoBean = recommend_info.get(position);
        Glide.with(mActivity).load(Constants.BASE_URL_IMAGE + recommendInfoBean.getFigure()).into(holder.iv_recommend);
        holder.tv_name.setText(recommendInfoBean.getName());
        holder.tv_price.setText("¥"+recommendInfoBean.getCover_price());

        return convertView;
    }

    class ViewHolder {

        private final ImageView iv_recommend;
        private final TextView tv_name;
        private final TextView tv_price;

        public ViewHolder(View view) {
            iv_recommend = (ImageView) view.findViewById(R.id.iv_recommend);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
        }
    }
}
